package com.cennavi.mineplay.data;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

import no.ecc.vectortile.VectorTileEncoder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.mineplay.utils.JsonUtils;
import com.mercator.TileUtils;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

@RestController
@RequestMapping(value = "/test")
public class od {

	@Autowired
	protected JdbcTemplate jdbc;

	static Random rand = new Random();

	@RequestMapping(value = "/test")
	public Object test() {
		return 333;
	}

	@RequestMapping(value = "/pbf/{z}/{x}/{y}", produces = "application/x-protobuf")
	public byte[] pbf(@PathVariable int x, @PathVariable int y,
			@PathVariable int z, String name) {

		VectorTileEncoder vte = new VectorTileEncoder(4096, 16, false);

		for (int i = 0; i < 22; i++) {

			Map<String, Object> m = new HashMap<String, Object>();

			m.put("id", i % 5);

			int lng = (int) (4096 * rand.nextDouble());

			int lat = (int) (4096 * rand.nextDouble());

			Geometry geom = null;
			try {
				geom = new WKTReader().read("Point (" + lng + " " + lat + ")");
			} catch (ParseException e) {
				e.printStackTrace();
			}

			// TileUtils.convert2Piexl(x, y, z, geom);

			vte.addFeature("position", m, geom);

		}

		byte[] value = vte.encode();

		return value;
	}

	@RequestMapping(value = "/pbf2/{z}/{x}/{y}", produces = "application/x-protobuf")
	public byte[] pbf2(@PathVariable int x, @PathVariable int y,
			@PathVariable int z, String name) {

		VectorTileEncoder vte = new VectorTileEncoder(4096, 16, false);

		for (int i = 0; i < 4096; i += 70) {
			for (int j = 0; j < 4096; j += 70) {

				Map<String, Object> m = new HashMap<String, Object>();

				m.put("i", i);
				m.put("j", j);

				int lng = i;

				int lat = j;

				Geometry geom = null;
				try {
					geom = new WKTReader().read("Point (" + lng + " " + lat
							+ ")");
				} catch (ParseException e) {
					e.printStackTrace();
				}

				// TileUtils.convert2Piexl(x, y, z, geom);

				vte.addFeature("position", m, geom);
			}
		}

		byte[] value = vte.encode();

		return value;
	}

	@RequestMapping(value = "/ab", produces = "application/x-protobuf")
	public byte[] ab() {

		ByteArrayOutputStream abo = new ByteArrayOutputStream();

		try {
			for (int i = 0; i < 1000000; i++)
			// abo.write(od.intToBytes2(99999));
			{
				double lng = 0 + (180 * Math.random());
				double lat = 0 + (90 * Math.random());

				int lng_int = (int) (lng * 1000000);
				int int_lat = (int) (lat * 1000000);

				abo.write(od.intToBytes2(lng_int));
				abo.write(od.intToBytes2(int_lat));

				// if (i % 2 == 0)
				// abo.write(-1);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return abo.toByteArray();

	}

	@RequestMapping(value = "/ab2", produces = "application/x-protobuf")
	public byte[] ab2() {

		ByteArrayOutputStream abo = new ByteArrayOutputStream();

		try {

			Scanner scanner = new Scanner(
					new FileInputStream(
							"/APP/docker/overlay2/8629f920ad6078bb28fc2cd0eedd6138db65fe90afa3f5e9473a116a527fab53/merged/mineplay3.0/shenzhen3.geojson"));

			StringBuilder sb = new StringBuilder();

			while (scanner.hasNextLine()) {
				sb.append(scanner.nextLine());
			}

			scanner.close();

			JSONObject jo = JSONObject.parseObject(sb.toString());

			JSONArray ja = jo.getJSONArray("features");

			for (int i = 0; i < ja.size(); i++) {
				JSONObject f = ja.getJSONObject(i);

				// if (f.getJSONObject("properties").getIntValue("LINK_PID") !=
				// 88636255)
				// continue;

				JSONArray ps = f.getJSONObject("geometry").getJSONArray(
						"coordinates");

				for (int j = 0; j < ps.size(); j++) {
					JSONArray p = ps.getJSONArray(j);

					double lng = p.getDoubleValue(0);
					double lat = p.getDoubleValue(1);

					int lng_int = (int) (lng * 1000000);
					int int_lat = (int) (lat * 1000000);

					abo.write(od.intToBytes2(lng_int));
					abo.write(od.intToBytes2(int_lat));
				}

				abo.write(-1);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return abo.toByteArray();

	}

	@RequestMapping(value = "/od", produces = "application/x-protobuf")
	public byte[] od2() {

		ByteArrayOutputStream abo = new ByteArrayOutputStream();

		try {

			Scanner scanner = new Scanner(
					new FileInputStream(
							"/APP/docker/overlay2/8629f920ad6078bb28fc2cd0eedd6138db65fe90afa3f5e9473a116a527fab53/merged/mineplay3.0/od.geojson"));

			StringBuilder sb = new StringBuilder();

			while (scanner.hasNextLine()) {
				sb.append(scanner.nextLine());
			}

			scanner.close();

			JSONObject jo = JSONObject.parseObject(sb.toString());

			JSONArray ja = jo.getJSONArray("features");

			for (int i = 0; i < ja.size(); i++) {
				JSONObject f = ja.getJSONObject(i);

				JSONArray ps = f.getJSONObject("geometry")
						.getJSONArray("coordinates").getJSONArray(0);

				for (int j = 0; j < ps.size(); j++) {
					JSONArray p = ps.getJSONArray(j);

					double lng = p.getDoubleValue(0);
					double lat = p.getDoubleValue(1);

					int lng_int = (int) (lng * 1000000);
					int int_lat = (int) (lat * 1000000);

					abo.write(od.intToBytes2(lng_int));
					abo.write(od.intToBytes2(int_lat));
				}

				abo.write(-1);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return abo.toByteArray();

	}

	@RequestMapping(value = "/test2")
	public Object test2(String a) {
		return JsonUtils.condition2String(a);
	}

	
	static int size = 100000;
	
	static int[] lng_int_array = new int[size];
	static int[] lat_int_array = new int[size];
	
	static boolean flag = false;
	
	static int py = 0;
	
	@RequestMapping(value = "/boat", produces = "application/x-protobuf")
	public byte[] boat() {
		
		py++;
		
		ByteArrayOutputStream abo = new ByteArrayOutputStream();
		
		try {
			
			if (!flag ){
				
				for(int i=0;i<size;i++)
//					abo.write(od.intToBytes2(99999));
					{
						double lng = 100 +  (4 * Math.random());
						double lat = 30 + (4 * Math.random());
						
						int lng_int = (int)(lng * 1000000);
						int int_lat = (int)(lat * 1000000);
						
						lng_int_array[i] = lng_int;
						lat_int_array[i] = int_lat;
						
//						
					}
				flag = true;
			}else{
				if (py <=15){
					for(int i=0;i<size;i++)
//						abo.write(od.intToBytes2(99999));
						{
							 
							
							lng_int_array[i] += 1115510;
							 
							
//							
						}
				}else{
					for(int i=0;i<size;i++)
//						abo.write(od.intToBytes2(99999));
						{
							 
							
							lng_int_array[i] -= 1115510;
							 
							
//							
						}
					if (py >= 30)
						py = 0;
				}
				
				
			}
			
			for(int i=0;i<lng_int_array.length;i++){
				abo.write(od.intToBytes2(lng_int_array[i]));
				abo.write(od.intToBytes2(lat_int_array[i]));
//				if (i % 2 == 0)
//					abo.write(-1);
			}
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return abo.toByteArray();
		
	}
	
	
	public static byte[] intToBytes2(int n) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			b[i] = (byte) (n >> (24 - i * 8));
		}
		return b;
	}

}
